<?php
/*
 * @Author: your name
 * @Date: 2021-11-02 20:43:06
 * @LastEditTime: 2022-02-06 17:30:08
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \property\app\Http\Controllers\admin_controller\BaseController.php
 */

namespace App\Http\Controllers\daily_partTime;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Request;

use App\Http\Controllers\redis\RedisController as Redis;
use App\Models\daily_partTime\User;

class BaseController extends Controller
{
    public $dbNumber = 12;
    public $service_redis = PARTTIME_SERVICE_REDIS_KEY;           //客服缓存集合       客服当前所有fd
    public $online_key    = __REDIS_ONLIE_KEY__;                  //当前fd绑定openid      1对1
    public $customRedisKey    = PARTTIME_CUSTOM_REDIS_KEY;        //用户集合





    /**
     * @description: 返回参数
     * @param int    $code       状态码
     * @param string $msg        返回信息
     * @param array  $data       参数
     * @param int    $trueCode   真状态码
     * @return 
     */
    protected function create($code, $msg = '', $data = [], $trueCode = 200)
    {
        $result = [
            'code' => $code,
            'message' => $msg,
            'data' => $data
        ];
        // if ($trueCode == 200) {
        //     return response($result, $trueCode);
        // } else {
            $_POST['HTTP_SERVER']->status($trueCode);
            return response($result);
        // }
    }


    protected function getLoginMsg($token = null)
    {
        $token =$token?$token: $_SERVER['TOKEN'];
        $key = PARTTIME_LOGIN_REDIS_KEY . $token;
        $openid =  Redis::hGET($key, 'openid', ['RedisDB' => 12]);
        return $openid;
    }

    

    /**
     * @description:  获取用户信息
     * @param {*} $field
     * @return {*}
     */    
    protected function getLoginAllMsg($token = null,$field = false)
    {
        $token = $token ?$token :$_SERVER['TOKEN'];
        $key = PARTTIME_LOGIN_REDIS_KEY . $token;
        $openid = $this->getLoginMsg($token);
        if( $openid){
            $res = Redis::hGET($key, 'id', ['RedisDB' => 12]);  //判断有无id
            if($res){
                $user_id =   $res;
                $data =  redis_mysql_selone_return('daily_parttime_user',$user_id,$field);
            }else{
                $data = User::loginId($openid);
                Redis::hMset( $key,['id'=> $data['id'] ], ['RedisDB' => 12]);
            }
         
            $data['token'] =  $token;
            return  $data ;

        }else{
            return false;
        }
      
      
    }
}
